package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.system.domain.WbDemandList;
import org.dromara.system.domain.bo.SkyRelationshipBo;
import org.dromara.system.domain.vo.WbCandidateListRiskVo;
import org.dromara.system.mapper.SkyNodeMapper;
import org.dromara.system.mapper.SkyRelationshipMapper;
import org.dromara.system.utils.SkyEyeClientUtil;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.WbCandidateListBo;
import org.dromara.system.domain.vo.WbCandidateListVo;
import org.dromara.system.domain.WbCandidateList;
import org.dromara.system.mapper.WbCandidateListMapper;
import org.dromara.system.service.IWbCandidateListService;

import java.util.*;

/**
 * 中标候选人详情Service业务层处理
 *
 * @author Lion Li
 * @date 2024-05-27
 */
@RequiredArgsConstructor
@Service
public class WbCandidateListServiceImpl implements IWbCandidateListService {

    private final WbCandidateListMapper baseMapper;
    private final SkyRelationshipMapper relationshipMapper;
    private final SkyNodeMapper nodeMapper;

    /**
     * 查询中标候选人详情
     */
    @Override
    public WbCandidateListVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询中标候选人详情列表
     */
    @Override
    public TableDataInfo<WbCandidateListVo> queryPageList(WbCandidateListBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WbCandidateList> lqw = buildQueryWrapper(bo);
        Page<WbCandidateListVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询中标候选人详情列表
     */
    @Override
    public List<WbCandidateListVo> queryList(WbCandidateListBo bo) {
        LambdaQueryWrapper<WbCandidateList> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WbCandidateList> buildQueryWrapper(WbCandidateListBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WbCandidateList> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCandidateId() != null, WbCandidateList::getCandidateId, bo.getCandidateId());
        lqw.eq(StringUtils.isNotBlank(bo.getSubStandardId()), WbCandidateList::getSubStandardId, bo.getSubStandardId());
        lqw.like(StringUtils.isNotBlank(bo.getSubStandardName()), WbCandidateList::getSubStandardName, bo.getSubStandardName());
        lqw.eq(StringUtils.isNotBlank(bo.getBagNo()), WbCandidateList::getBagNo, bo.getBagNo());
        lqw.like(StringUtils.isNotBlank(bo.getBagName()), WbCandidateList::getBagName, bo.getBagName());
        lqw.eq(StringUtils.isNotBlank(bo.getCandidate()), WbCandidateList::getCandidate, bo.getCandidate());
        lqw.eq(bo.getPriceOrDiscount() != null, WbCandidateList::getPriceOrDiscount, bo.getPriceOrDiscount());
        lqw.eq(StringUtils.isNotBlank(bo.getQuality()), WbCandidateList::getQuality, bo.getQuality());
        lqw.eq(StringUtils.isNotBlank(bo.getDelivery()), WbCandidateList::getDelivery, bo.getDelivery());
        lqw.eq(bo.getSort() != null, WbCandidateList::getSort, bo.getSort());
        lqw.eq(StringUtils.isNotBlank(bo.getQualified()), WbCandidateList::getQualified, bo.getQualified());
        lqw.eq(StringUtils.isNotBlank(bo.getBidCase()), WbCandidateList::getBidCase, bo.getBidCase());
        lqw.eq(StringUtils.isNotBlank(bo.getItemUnit()), WbCandidateList::getItemUnit, bo.getItemUnit());
        lqw.orderByAsc(WbCandidateList::getSort,WbCandidateList::getSubStandardId,WbCandidateList::getBagNo,WbCandidateList::getId);
        return lqw;
    }

    /**
     * 新增中标候选人详情
     */
    @Override
    public Boolean insertByBo(WbCandidateListBo bo) {
        WbCandidateList add = MapstructUtils.convert(bo, WbCandidateList.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改中标候选人详情
     */
    @Override
    public Boolean updateByBo(WbCandidateListBo bo) {
        WbCandidateList update = MapstructUtils.convert(bo, WbCandidateList.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WbCandidateList entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除中标候选人详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public TableDataInfo<WbCandidateListVo> queryByList(String year,String provinceId , Long enterpriseId, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.selectByList(year,provinceId,enterpriseId,pageQuery.build()));
    }

    @Override
    public List<WbCandidateListVo> queryList1(WbCandidateListBo bo) {
        LambdaQueryWrapper<WbCandidateList> lqw =  Wrappers.lambdaQuery();
        lqw.eq(bo.getCandidateId() != null, WbCandidateList::getCandidateId, bo.getCandidateId());
        lqw.eq(StringUtils.isNotBlank(bo.getSubStandardName()), WbCandidateList::getSubStandardName, bo.getSubStandardName());
        lqw.eq(StringUtils.isNotBlank(bo.getBagNo()), WbCandidateList::getBagNo, bo.getBagNo());
        lqw.eq(StringUtils.isNotBlank(bo.getSubStandardId()), WbCandidateList::getSubStandardId, bo.getSubStandardId());
        lqw.eq(StringUtils.isNotBlank(bo.getCandidate()), WbCandidateList::getCandidate, bo.getCandidate());
        return baseMapper.selectVoList(lqw);
    }

    @Override
    public TableDataInfo queryCandidatePageList(WbCandidateListBo bo, PageQuery pageQuery) {
        if(StringUtils.isNotBlank(bo.getCandidate())) {
            bo.setCandidate("%"+bo.getCandidate()+"%");
        }

        Page page = null;
        switch (bo.getType()) {
            case "0" : page = baseMapper.queryCandidatePageList(bo, pageQuery.build()); break;
            case "1" : page = baseMapper.queryAbnormalPage(bo, pageQuery.build()); break;
            case "2" : page = baseMapper.queryIllegalPage(bo, pageQuery.build()); break;
            case "3" : page = baseMapper.queryDishonestPage(bo, pageQuery.build()); break;
            case "4" : page = baseMapper.queryPunishmentPage(bo, pageQuery.build()); break;
            case "5" : page = baseMapper.queryBlackListPage(bo, pageQuery.build()); break;
        }

        if (page != null) {
            return TableDataInfo.build(page);
        }

        return TableDataInfo.build();
    }

    @Override
    public Boolean mergeByBo(WbCandidateListBo bo) {
        return baseMapper.mergeByBo(bo) > 0;
    }

    @Override
    public List<String> queryAllCandidate(WbCandidateListBo bo) {
        return baseMapper.queryAllCandidate(bo);
    }

    @Override
    public List<WbCandidateListRiskVo> queryRiskList(WbCandidateListBo bo) {
        return baseMapper.queryRiskList(bo);
    }

    @Override
    public void removeByCandidateId(Long candidateId) {
        if(candidateId != null) {
            baseMapper.delete(new LambdaQueryWrapper<WbCandidateList>().eq(WbCandidateList::getCandidateId, candidateId));
        }
    }
}
